<?php 
session_start();

require("../../conexao/ConectaMysql.class.php");
require("../../conexao/ControleAcesso.class.php");

require("../setor/setor.class.php");
require("../setor/gerencia_setor.class.php");

require("../privilegio/privilegio.class.php");
require("../privilegio/gerencia_privilegio.class.php");

require("../acesso/acesso.class.php");
require("../acesso/gerencia_acesso.class.php");

require("../funcionario/funcionario.class.php");
require("../funcionario/gerencia_funcionario.class.php");

ControleAcesso::verificaSessao();

$ACE_PRIVid = $_SESSION['PRIVid']; 
$ACE_SETid = $_SESSION['SETid'];

$conexao = new ConectaMysql();
$gerenciaSetor = new GerenciaSetor($conexao);
$gerenciaPrivilegio = new GerenciaPrivilegio($conexao);
$gerenciaFuncionario = new GerenciaFuncionario($conexao);

if (isset($_POST['Alterar'])){

	$FUNid 			= $_POST['FUNid'];
	$FUNnome		= $_POST['FUNnome'];
	$FUNsobrenome 	= $_POST['FUNsobrenome'];
	$FUNstatus		= $_POST['FUNstatus'];
	$ACElogin 		= $_POST['FUNlogin'];
	$ACEsenha 		= $_POST['FUNsenha'];
	$PRIVid 		= $_POST['PRIVid'];
	$SETid 			= $_POST['SETid'];
	
	////////////////////SETOR/////////////////////////
	$result_setor = $gerenciaSetor->selectSetor($SETid);
	foreach($result_setor as $row_setor){  
		$SETnome	= $row_setor["SETnome"];
		$SETsigla	= $row_setor["SETsigla"];
	}
	
	$objectSetor = new setor($SETid, $SETnome, $SETsigla);
	
	////////////////funcionario////////////////////////
	$gerenciafuncionario = new GerenciaFuncionario($conexao);
	
	$objectfuncionario = new Funcionario($FUNid, $FUNnome, $FUNsobrenome, $objectSetor, $FUNstatus);
	$gerenciafuncionario->updateFuncionario($objectfuncionario);
	
	///////////////////////////////////////////////////
	
	//////////////////Privilegio//////////////////////
	
	$result_privilegio  = $gerenciaPrivilegio->selectPrivilegio($PRIVid);
	foreach($result_privilegio as $row_privilegio){  
		$PRIVdescricao =  $row_privilegio["PRIVdescricao"];
	}
	
	$objectPrivilegio = new Privilegio($PRIVid, $PRIVdescricao);
	
	//////////////////////////////////////////////////
	
	
	/////////////////////Acesso///////////////////////
	$objectAcesso = new Acesso($objectfuncionario, $objectPrivilegio, $ACElogin, $ACEsenha);
	$gerenciaAcesso = new GerenciaAcesso($conexao);
	$gerenciaAcesso->updateAcesso($objectAcesso);
	
	////////////////////////////////////////////////////
	
	echo "<script language=JavaScript>";
	echo "alert('Alterado com sucesso');";
	echo "location.href = 'formulario.php';";
	echo "</script>";	
}


else if (isset($_GET['FUNid'])){
	
	$FUNid = $_GET['FUNid'];
	
	$result_funcionario = $gerenciaFuncionario->selectFuncionario($FUNid, true);
	foreach($result_funcionario as $row_funcionario){
		$FUNid      	= $row_funcionario["FUNid"];
		$FUNnome   		= $row_funcionario["FUNnome"];
		$FUNsobrenome	= $row_funcionario["FUNsobrenome"];
		$FUN_SETid 		= $row_funcionario["FUN_SETid"];
		$FUNstatus		= $row_funcionario["FUNstatus"];
		$FUN_PRIVid 	= $row_funcionario["ACE_PRIVid"];
		$FUNlogin		= $row_funcionario["ACElogin"];
		$FUNsenha		= $row_funcionario["ACEsenha"];
	}
	
	
	?>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
	<html>
	<head>
	<title>Funcion&aacute;rio</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<link href="../../estrutura/css/estruturaCadastro.css" rel="stylesheet" type="text/css" media="screen" />
	<script type="text/javascript" src="../../estrutura/js/jquery.js"></script>
	<script type="text/javascript" src="../../estrutura/js/jquery.validate.js"></script>
	<script type="text/javascript" src="../../estrutura/js/validacaoFuncionario.js"></script>
	</head>
	
	<body>
	<div align="left">
		<form action="alterar_funcionario.php" method="post" name="formFuncionario" id="formFuncionario" target="_self">
		<input id="FUNid" name="FUNid" type="hidden" value="<?php echo "$FUNid";?>"/>
			<table width="90%"  border="0" align="center">
				<tr>
					<td colspan="2" bgcolor="#DFEDF7" class="texto"><div align="left"></div>
					<strong>Informe os dados do Funcion&aacute;rio</strong></td>
				</tr>
				<tr>
					<td colspan="2" class="texto"><span class="style50">* campos obrigat&oacute;rios</span></td>
				</tr>
				<tr>
					<td width="21%" bgcolor="#FFFFFF" class="texto">&nbsp;</td>
					<td width="79%">&nbsp;</td>
				</tr>
				<tr>
					<td bgcolor="#F1F4F8" class="texto">Nome: </td>
					<td>
						<span class="style50">
						<label>
							<input type="text" name="FUNnome" id="FUNnome" value="<?php echo "$FUNnome";?>"  <?php if($ACE_PRIVid == '3'){ echo "readonly";} ?>>
						</label>
						*</span>
					</td>
				</tr>
				<tr>
					<td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Sobrenome:</span></td>
					<td>
						<span class="style50">
						<label>
						<input type="text" name="FUNsobrenome" id="FUNsobrenome" value="<?php echo "$FUNsobrenome ";?>"  <?php if($ACE_PRIVid == '3'){ echo "readonly";} ?>>
						</label>
						*</span>        
					</td>
				</tr>
				<tr>
				  <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Login:</span></td>
				  <td><span class="style50">
					<label>
					  <input type="text" name="FUNlogin" id="FUNlogin" value="<?php echo "$FUNlogin";?>"  <?php if($ACE_PRIVid == '3'){ echo "readonly";} ?>>
					</label>
					*</span></td>
				</tr>
				<tr>
				  <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Senha:</span></td>
				  <td><span class="style50">
					<label>
					  <input type="password" name="FUNsenha" id="FUNsenha" value="<?php echo "$FUNsenha";?>">
					</label>
					*</span></td>
				</tr>
				<tr>
				  <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Repetir Senha:</span></td>
				  <td><span class="style50">
					<label>
					  <input type="password" name="FUNrepetir_senha" id="FUNrepetir_senha" value="<?php echo "$FUNsenha";?>">
					</label>
					*</span></td>
				</tr>
				<tr>
				  <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Setor:</span></td>
				  <td>
					<span class="style50">
						<select id="SETid" name="SETid">
							<?php
							$result_setor = $gerenciaSetor->selectSetor($ACE_SETid, $ACE_PRIVid);
							foreach($result_setor as $row_setor){  
								$SETid       = $row_setor["SETid"];
								$SETsigla    = $row_setor["SETsigla"];
							?>
                            <option value="<?php echo "$SETid"; ?>" <?php if ($FUN_SETid == $SETid){echo "SELECTED";}?>>
                                <?php echo "$SETsigla"; ?>
                            </option>
							<?php }?>
						</select>
					*</span>
				  </td>
				</tr>
				<?php if($ACE_PRIVid == 1){?>
                    <tr>
                      <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Privil&eacute;gio:</span></td>
                      <td><span class="style50">
                        <select id="PRIVid" name="PRIVid">
                                <?php				
                                $result_privilegio = $gerenciaPrivilegio->selectPrivilegio();
                                foreach($result_privilegio as $row_privilegio){  
                                    $PRIVid        =  $row_privilegio["PRIVid"];
                                    $PRIVdescricao =  $row_privilegio["PRIVdescricao"];
                                ?>
                                    <option value="<?php echo "$PRIVid "; ?>" <?php if ($FUN_PRIVid == $PRIVid){echo "SELECTED";}?>>
                                        <?php echo "$PRIVdescricao"; ?>
                                    </option>
                                
                                <?php }?>							
                        </select>
                      *</span></td>
                    </tr>
                <?php }else{ echo "<input type=\"hidden\" id=\"PRIVid\" name=\"PRIVid\" value=\"$FUN_PRIVid\">"; }?>
				<tr>
				  <td bgcolor="#F1F4F8" class="texto style41"><span class="texto">Status:</span></td>
				  <td><span class="style50">
					<select id="FUNstatus" name="FUNstatus" <?php if($ACE_PRIVid <> '1'){ ?> onchange="this.options[0].selected = true;" <?php } ?>>
					  <option value="1" <?php if ($FUNstatus == 1){ echo "SELECTED"; } ?>>Ativo</option>
					  <option value="0" <?php if ($FUNstatus == 0){ echo "SELECTED"; } ?>>Inativo</option>
					</select>
				  *</span></td>
				</tr>
				<tr>
					<td class="texto">&nbsp;</td>
					<td>&nbsp; </td>
				</tr>
				<tr>
					<td class="texto style42">&nbsp;</td>
					<td>
						<input name="Alterar" type="submit" id="Alterar" value="Confirmar" class="caixa_texto">&nbsp;&nbsp;&nbsp;
						<input name="voltar" type="button" id="voltar" value="Voltar" class="caixa_texto" OnClick="location.href='formulario.php'"/>         			</td>
				</tr>
			</table>
		</form>
	</div>
	</body>
	</html>
<?php } ?>